<template>
  <a-layout>
    <!-- 头部 -->
    <a-layout-header class="header">
      <div class="logo">wiki知识库</div>
      <a-menu theme="dark" mode="horizontal" :style="{ lineHeight: '64px' }">
        <a-menu-item key="/">
          <router-link to="/">首页</router-link>
        </a-menu-item>
        <a-menu-item key="/admin/user" v-if="user.id">
          <router-link to="/admin/user">用户管理</router-link>
        </a-menu-item>
        <a-menu-item key="/admin/category" v-if="user.id">
          <router-link to="/admin/category">分类管理</router-link>
        </a-menu-item>
        <a-menu-item key="/admin/ebook" v-if="user.id">
          <router-link to="/admin/ebook">电子书管理</router-link>
        </a-menu-item>
        <a-menu-item key="/about">
          <router-link to="/about">关于</router-link>
        </a-menu-item>

        <a-menu-item key="user_Name" style="margin-left: auto">
          <a class="login-menu" v-show="user.id != undefined && user.id != ''">
            <span>当前用户：{{ user.name }}</span>
          </a>
        </a-menu-item>
        <!-- 注销登录 -->
        <a-menu-item key="user_logout" style="margin-left: auto">
          <a-popconfirm
            title="确认退出登录?"
            ok-text="是"
            cancel-text="否"
            @confirm="logout()"
          >
            <a class="login-menu" v-show="user.id">
              <span>退出登录</span>
            </a>
          </a-popconfirm>
        </a-menu-item>
        <!-- 登录按钮 -->
        <a-menu-item key="login" style="margin-left: auto">
          <a
            class="login-menu"
            v-show="user.id == undefined || user.id == ''"
            @click="showLoginModal"
          >
            <span>登录</span>
          </a>
        </a-menu-item>
      </a-menu>

      <!-- 登录的对话框 -->
      <a-modal
        title="登录"
        v-model:visible="loginModalVisible"
        :confirm-loading="loginModalLoading"
        @ok="login"
      >
        <a-form
          :model="loginUser"
          :label-col="{ span: 6 }"
          :wrapper-col="{ span: 18 }"
        >
          <a-form-item label="登录名">
            <a-input v-model:value="loginUser.loginName" />
          </a-form-item>
          <a-form-item label="密码">
            <a-input v-model:value="loginUser.password" type="password" />
          </a-form-item>
        </a-form>
      </a-modal>
    </a-layout-header>
  </a-layout>
</template>

<script lang="ts">
import { defineComponent, ref, computed } from "vue";

import axios from "axios";
import { message } from "ant-design-vue";
import store from "@/store";

declare let hexMd5: any;
declare let KEY: any;

export default defineComponent({
  name: "the-header",
  setup() {
    //从store中获取user对象数据
    const user = computed(() => store.state.user);
    // 用来登录
    const loginUser = ref({
      loginName: "test",
      password: "admin123456",
    });
    const loginModalVisible = ref(false);
    const loginModalLoading = ref(false);
    const showLoginModal = () => {
      loginModalVisible.value = true;
    };

    //登录
    const login = () => {
      console.log("登录开始");
      loginModalLoading.value = true;
      //第一次加密
      loginUser.value.password = hexMd5(loginUser.value.password + KEY);
      //发起请求
      axios.post("/user/userLogin", loginUser.value).then((response) => {
        loginModalLoading.value = false;
        const data = response.data;
        if (data.success) {
          //把user的信息赋值到store中
          store.commit("setUser", data.content);
          loginModalVisible.value = false;
          message.success("登录成功！");
        } else {
          message.error(data.message);
        }
      });
    };

    // 退出登录
    const logout = () => {
      console.log("退出登录开始");
      axios.get("/user/logout/" + user.value.token).then((response) => {
        const data = response.data;
        if (data.success) {
          message.success("退出登录成功！");
          store.commit("setUser", {});
        } else {
          message.error(data.message);
        }
      });
    };

    return {
      loginModalVisible,
      loginModalLoading,
      showLoginModal,
      loginUser,
      login,
      user,
      logout,
    };
  },
});
</script>

<style>
#webpack-dev-server-client-overlay {
  display: none !important;
}

.logo {
  width: 120px;
  height: 31px;
  /*background: rgba(255, 255, 255, 0.2);*/
  /*margin: 16px 28px 16px 0;*/
  float: left;
  color: white;
  font-size: 18px;
}
.login-menu {
  float: right;
  color: white;
  padding-left: 10px;
}
</style>
